from django.shortcuts import render
from django.views import View
from goods.models import GoodsCategory,GoodsChannel
from .utils import get_categories
from .models import Content,ContentCategory

# Create your views here.

class IndexView(View):

    def get(self, request):

        # categories = {}
        #
        # goods_channel_qs = GoodsChannel.objects.order_by('group_id', 'sequence')
        #
        # for channel_model in goods_channel_qs:
        #     group_id = channel_model.group_id
        #     if group_id not in categories:
        #         categories[group_id] = {
        #             'channels': [],
        #             'sub_cats': []
        #         }
        #     cat1 = channel_model.category
        #     cat1.url = channel_model.url
        #     categories[group_id]['channels'].append(cat1)
        #
        #     cat2_qs = cat1.subs.all()
        #     for cat2 in cat2_qs:
        #         cat3_qs = cat2.subs.all()
        #         cat2.sub_cats = cat3_qs
        #
        #         categories[group_id]['sub_cats'].append(cat2)

        contents = {}
        content_cat_qs = ContentCategory.objects.all()

        for content_cat in content_cat_qs:
            contents[content_cat.key] = content_cat.content_set.filter(status=True).order_by('sequence')


        context = {
            # 商品分类数据
            'categories': get_categories(),
            # 广告数据
            'contents': contents
        }
        return render(request, 'index.html', context)